﻿using System;
using System.Xml.Linq;
using ClrExtensions.Net.Rest;
using MediaPortal.GUI.Library;

namespace MyNetflix2
{
    public class Recommendations : Queue
    {
        private User _user = null;
        
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="user"></param>
        public Recommendations(User user)
            : base(user)
        { _user = user; }

        /// <summary>
        /// Refresh the Recommendations Queue
        /// </summary>
        /// <param name="resource"></param>
        /// <param name="accessToken"></param>
        /// <returns></returns>
        public override void Refresh()
        {
            //--------------------------------------------------------------------------------
            // Create the Rest call
            //--------------------------------------------------------------------------------
            RestCall request = RestCalls.GetRecommendations(base.NetFlixUser.Token, StartIndex, EndIndex);

            try
            {
                //--------------------------------------------------------------------------------
                // Get the Recommendations Queue from Netflix
                //--------------------------------------------------------------------------------
                string xml = base.NetFlixUser.NetflixConnection.RequestResource(request, base.NetFlixUser.Token);

                //--------------------------------------------------------------------------------
                // Parse to Linq
                //--------------------------------------------------------------------------------
                XElement root = XElement.Parse(xml);

                //--------------------------------------------------------------------------------
                // Add the elements to the QueueItems class
                //--------------------------------------------------------------------------------
                foreach (XElement node in root.Elements("recommendation"))
                {
                    QueueItems.Add(new QueueItem(node, _user));
                }
            }
            catch (Exception ex)
            {
                Log.Error("My NetFlix - Recommendations:Refresh(): Error - " + ex.Message);
            }
        }
    }
}
